﻿using System;
using System.Collections.Generic;
using System.Windows;
using SomethingSpacial.ViewModel;
using SomethingSpacial.Views;
using SomethingSpacial.Views.Default;

namespace SomethingSpacial
{
    public partial class App : Application
    {
        public static ExternalFeed MainFeed = new ExternalFeed();
        public static User CurrentUser = new User();
        public static IDictionary<string, string> Settings = null;

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        /// <summary>
        /// designed to set the view.  SL3 disabled this to some degree and needs to be moved to a Shell method for view switching.
        /// </summary>
        /// <param name="ViewKey"></param>
        public static void SetView(string ViewKey)
        {
            switch (ViewKey)
            {
                case "Default":
                    //App.Current.RootVisual = new DefaultView();
                    App.Current.RootVisual = new SomethingSpacial.Views.Admin.AdminContainer();
                    break;
                case "LoginForm":
                    App.Current.RootVisual = new LoginForm();
                    break;
            }
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Settings = e.InitParams;

            string URL = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri;
            MainFeed.Address = URL.Substring(0, URL.LastIndexOf('/')) + "/ServiceProxy.aspx?ServicePath=" + Settings["MainRSSFeed"];

            Settings["Newsfeed"] = URL.Substring(0, URL.LastIndexOf('/')) + "/ServiceProxy.aspx?ServicePath=" + Settings["Newsfeed"];

            //MainFeed.Address = Settings["MainRSSFeed"];

            SetView(Settings["RootVisual"]);
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }
    }
}
